package cn.virens.server.aio.handler;

import cn.virens.server.aio.channel.AioChannel;
import cn.virens.server.common.ReadPacket;

public class PacketHandlerSimple extends PacketHandler {
	private final Handler handler;
	private final int support;

	private PacketHandlerSimple(int support, Handler handler) {
		this.handler = handler;
		this.support = support;
	}

	@Override
	public boolean can(int cmd) throws Exception {
		return support == -1 || support == cmd;
	}

	@Override
	public void handle(AioChannel channel, ReadPacket packet) throws Exception {
		this.handler.handle(channel, packet);
	}

	public static PacketHandler of(int cmd, Handler handler) {
		return new PacketHandlerSimple(cmd, handler);
	}

	public interface Handler {
		void handle(AioChannel channel, ReadPacket packet) throws Exception;
	}
}
